package com.moment.controller;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;



/**
 * Servlet implementation class PaymentRequestController
 */
@WebServlet("/PaymentRequestController")
public class PaymentRequestController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PaymentRequestController() {
        super();
        // TODO Auto-generated constructor stub
    }
    private void paymentrequest(HttpServletRequest request, HttpServletResponse response)
    {
    	String cnumber = request.getParameter("cnum");
    	String amount = request.getParameter("amount");
    	String mobilenumber=(String)request.getSession().getAttribute("username");
    	String target="jsp/viewprofile.jsp";
    	//System.out.println(amount+"--"+cnumber);
   
    	try {
    		//--New CODE-------------
			Map<String,String> map=new HashMap<String,String>();
			map.put("method", "12");
		map.put("customermnum",cnumber);
					map.put("merchantmnum",mobilenumber);
					map.put("type","merchant");
					map.put("amount",amount);

			String json=new Converter().mapToJson(map);
			System.out.println("json String :"+json);
			Services serviceproxy=new ServicesProxy();
			
			String output=serviceproxy.service(json);
//---------------------------
		//	String output = proxy.addNotification(cnumber, mobilenumber, "merchant", amount);
			if(output.equalsIgnoreCase("success"))
			{
				request.getSession().setAttribute("success", "Payment requested successfully.");
				target="jsp/viewprofile.jsp";
			}
			else
			{
				request.getSession().setAttribute("error", "Number Does not exist");
				target="jsp/viewprofile.jsp";
			}
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	try {
			response.sendRedirect(target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		paymentrequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		paymentrequest(request,response);
	}

}
